//index.js
//获取应用实例
const app = getApp();
let util = require('../../utils/util');
const api = require('../../utils/api');
app.global.isdefeat = false;

Page({
  data: {
    // 登录所需信息
    userInfo: {},
    token: wx.getStorageSync('token'),

    // 失败或成功的组件
    isdefeat: app.global.isdefeat,
    isSuccess: app.global.isSuccess,

    //轮播
    dots: true,
    autoplay: true, 
    interval: 3000,
    duration: 800, 
    imgUrls: "",
    swiperN: 8,//轮播图数量

    //优选家具
    allProUrls: "",
    furProUrls: "",
    furItemN: 2,//当前图片数量
    allItemN: 0,//图片总数量
    more: true,//判断是否还有图片

    //menu
    x: 750,
    y: 550,
    frictN: 10,
    showMenu: false,
    menuStyle: "",//menu出现样式 
    //search
    searchKey: ""//搜索的关键字
  },
  //搜索按钮
  toSearchList(){
    wx.navigateTo({
      url: '/pages/searchList/searchList',
    })
  },
  //搜索
  // searchKey(e){
  //   this.setData({
  //     searchKey: e.detail.value
  //   })
  // },
  // onSearch(){
  //   let that = this;
  //   let key = that.data.searchKey;
  //   if(key){
  //     wx.navigateTo({
  //       url: '/pages/search/search?keyword=' + key,
  //     })
  //   }else{
  //     wx.showToast({
  //       title: '请输入搜索关键词',
  //       icon: 'none'
  //     })
  //   }
    
  // },
  //右边菜单栏样式
  toMenu(){
    this.setData({
      menuStyle: 'right:'+0+';'+'transition:'+"300ms"
    })
  },
  closeMenu(){
    this.setData({
      menuStyle: 'right:-' + 100 + '%;' + 'transition:' + "200ms"
    })
  },
  onLoad: function () {
    var that = this;
    wx.showNavigationBarLoading();
    wx.showLoading({
      title: "加载中",
      mask: true
    });

    //获取轮播图
    let saveSwiper = wx.getStorageSync('imgUrls');
    if(saveSwiper){
      wx.hideNavigationBarLoading();
      wx.hideLoading();
      that.setData({
        imgUrls: saveSwiper
      })
    }else{
      util.myRequest(api.index,{},
        res=>{
          wx.hideNavigationBarLoading();
          wx.hideLoading();
          that.setData({
            imgUrls: res.data.data.banner
          });
          wx.setStorage({
            key: 'imgUrls',
            data: res.data.data.banner,
          })
        }
      )
    }

    //获取优选家具图片
    let savePro = wx.getStorageSync("allProUrls");
    if(savePro){
      that.setData({
        allItemN: savePro.length,
        allProUrls: savePro,
        furProUrls: savePro.slice(0, that.data.furItemN)
      })
    }else{
      util.myRequest(api.goods, {},
        res => {
          that.setData({
            allItemN: res.data.data.goodsList.length,
            allProUrls: res.data.data.goodsList,
            furProUrls: res.data.data.goodsList.slice(0, that.data.furItemN)
          });
          wx.setStorage({
            key: 'allProUrls',
            data: res.data.data.goodsList,
          })
        }
      )
    }
  },
  onShow: function() {
    this.setData({
      menuStyle: 'right:-' + 100 + '%;'
    })
  },
  //优选家具-查看更多
  seeMore() {
    var that = this;
    if (that.data.furItemN < that.data.allItemN){
      that.setData({
        furItemN: that.data.furItemN + 2
      },function(){
        that.setData({
          furProUrls: that.data.allProUrls.slice(0, that.data.furItemN)
        })
      })     
    }else{
      that.setData({
        more: false
      })
    }
  },

  //跳转到商品详情页面
  toProductInfo(e){
    let id = e.currentTarget.id;
    wx.navigateTo({
      url: "../productInfo/productInfo?id=" + this.data.allProUrls[id].id
    })
  },
  swiperToProInfo(e){
    let id = e.currentTarget.id;
    console.log(id);
    console.log(this.data.imgUrls)
    // wx.navigateTo({
    //   url: "../productInfo/productInfo?id=" + this.data.imgUrls[id].id
    // })
  },
  /**
   * 返回用户信息，并进行请求，拿到token
   * @param {用户信息对象} e 
   */
  getInfo(e) {
    let that = this;
    console.log(e.detail)
    // 登录
    util.login(e.detail,that);
    
    console.log(app.global.token);
    // app.global.userInfo = e.detail.userInfo;
  }
})
